package com.zheng.pay.admin.manage.controller;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.zheng.common.base.BaseController;
import com.zheng.common.util.*;
import com.zheng.common.validator.LengthValidator;
import com.zheng.pay.rpc.api.PayTransOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.zheng.pay.dao.model.*;
import com.zheng.pay.common.constant.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;

@Controller
@Api(value = "转账订单管理", description = "转账订单管理")
@RequestMapping("/manage/transorder")
public class TransOrderController extends BaseController{

    private static final Logger _log = LoggerFactory.getLogger(TransOrderController.class);

    @Autowired
    private PayTransOrderService payTransOrderService;

    @ApiOperation(value = "转账订单首页")
    @RequiresPermissions("pay:transorder:read")
    @RequestMapping(value = "/index", method = {RequestMethod.GET, RequestMethod.POST})
    public String index() {
        return "/manage/transorder/index.jsp";
    }

    @ApiOperation(value = "转账订单列表")
    @RequiresPermissions("pay:transorder:read")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order,
            @RequestParam(required = false, defaultValue = "", value = "orderid") String orderid,
            @RequestParam(required = false, defaultValue = "", value = "mchId") String mchId,
            @RequestParam(required = false, defaultValue = "", value = "begintime") String begintime,
            @RequestParam(required = false, defaultValue = "", value = "endtime") String endtime,
            @RequestParam(required = false, defaultValue = "", value = "mchOrderNo") String mchOrderNo) {

        PayTransOrderExample payTransOrderExample= new PayTransOrderExample();
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            payTransOrderExample.setOrderByClause(sort + " " + order);
        }
        List<PayTransOrder> rows = payTransOrderService.selectByExampleForOffsetPage(payTransOrderExample, offset, limit);
        long total = payTransOrderService.countByExample(payTransOrderExample);
        Map<String, Object> result = new HashMap<>(2);
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "新增")
    @RequiresPermissions("pay:transorder:create")
    @RequestMapping(value = "/create", method = {RequestMethod.GET})
    public String create() {
        return "/manage/transorder/create.jsp";
    }

    @ApiOperation(value = "新增转账订单")
    @RequiresPermissions("pay:transorder:create")
    @RequestMapping(value = "/create", method = {RequestMethod.POST})
    @ResponseBody
    public Object create(PayTransOrder payTransOrder) {
        ComplexResult result = FluentValidator.checkAll()
                .on(payTransOrder.getTransorderid(), new LengthValidator(1, 30, "转账订单号"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            return new PayResult(PayResultConstant.INVALID_LENGTH, result.getErrors());
        }
        Date time = DateUtil.getNowDate();
        payTransOrder.setUpdatetime(time);
        payTransOrder.setCreatetime(time);
        int count = payTransOrderService.insertSelective(payTransOrder);
        return new PayResult(PayResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "删除转账订单")
    @RequiresPermissions("pay:transorder:delete")
    @RequestMapping(value = "/delete/{ids}",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        int count = payTransOrderService.deleteByPrimaryKeys(ids);
        return new PayResult(PayResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "修改转账订单")
    @RequiresPermissions("pay:transorder:update")
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.GET})
    public String update(@PathVariable("id") String id, ModelMap modelMap) {

        PayTransOrderExample payTransOrderExample= new PayTransOrderExample();
        if (!StringUtils.isBlank(id) && !StringUtils.isBlank(id)) {
            payTransOrderExample.createCriteria().andTransorderidEqualTo(id);
        }
        PayTransOrder payTransOrder = payTransOrderService.selectFirstByExample(payTransOrderExample);
        modelMap.put("payTransOrder", payTransOrder);
        return "/manage/transorder/update.jsp";
    }

    @ApiOperation(value = "修改转账订单")
    @RequiresPermissions("pay:transorder:update")
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.POST})
    @ResponseBody
    public Object update(@PathVariable("id") String id, PayTransOrder payTransOrder) {
        ComplexResult result = FluentValidator.checkAll()
                .on(payTransOrder.getTransorderid(), new LengthValidator(1, 30, "转账订单号"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            return new PayResult(PayResultConstant.INVALID_LENGTH, result.getErrors());
        }
        payTransOrder.setTransorderid(id);
        int count = payTransOrderService.updateByPrimaryKeySelective(payTransOrder);
        return new PayResult(PayResultConstant.SUCCESS, count);
    }

}